<?php

namespace app\common\model\jeek;

use think\Model;


class Merchant extends Model
{

    

    

    // 表名
    protected $name = 'jeek_merchant';
    
    // 自动写入时间戳字段
    protected $autoWriteTimestamp = 'integer';

    // 定义时间戳字段名
    protected $createTime = 'createtime';
    protected $updateTime = 'updatetime';
    protected $deleteTime = false;

    // 追加属性
    protected $append = [
        'logintime_text',
        'status_text'
    ];
    

    protected static function init()
    {
        self::afterInsert(function ($row) {
            if (!$row['weigh']) {
                $pk = $row->getPk();
                $row->getQuery()->where($pk, $row[$pk])->update(['weigh' => $row[$pk]]);
            }
        });
    }

    
    public function getStatusList()
    {
        return ['30' => __('Status 30')];
    }


    public function getLogintimeTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['logintime']) ? $data['logintime'] : '');
        return is_numeric($value) ? date("Y-m-d H:i:s", $value) : $value;
    }


    public function getStatusTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['status']) ? $data['status'] : '');
        $list = $this->getStatusList();
        return isset($list[$value]) ? $list[$value] : '';
    }

    protected function setLogintimeAttr($value)
    {
        return $value === '' ? null : ($value && !is_numeric($value) ? strtotime($value) : $value);
    }


    public function admin()
    {
        return $this->belongsTo('app\common\model\Admin', 'pid', 'id', [], 'LEFT')->setEagerlyType(0);
    }

        /**
     * 读取等级类型
     * @return array
     */
    public static function getLevelsList()
    {
        $typeList = config('site.levels');
        foreach ($typeList as $k => &$v) {
            $v = __($v);
        }
        return $typeList;
    }

        /**
     * 读取颜色类型
     * @return array
     */
    public static function getMystyleList()
    {
        $typeList = config('site.mystyle');
        foreach ($typeList as $k => &$v) {
            $v = __($v);
        }
        return $typeList;
    }


}
